package com.human.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.human.server.entity.AttendanceCheckin;
import com.human.server.entity.Employees;
import com.human.server.mapper.EmployeesMapper;
import com.human.server.service.AttendanceCheckinService;
import com.human.server.mapper.AttendanceCheckinMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.List;

/**
* @author Admin
* @description 针对表【attendance_checkin(考勤打卡记录表)】的数据库操作Service实现
* @createDate 2025-09-30 00:14:55
*/
@Service
public class AttendanceCheckinServiceImpl extends ServiceImpl<AttendanceCheckinMapper, AttendanceCheckin>
    implements AttendanceCheckinService {

    @Autowired
    private EmployeesMapper employeesMapper;

    public void checkin(AttendanceCheckin attendanceCheckin) {

        // 检查是否已打卡
        Employees employee = employeesMapper.selectOne(new LambdaQueryWrapper<Employees>()
                .eq(Employees::getEmployeeId, attendanceCheckin.getEmployeeId()));
        if (employee == null) {
            throw new IllegalArgumentException("员工不存在");
        }

        // 保存打卡记录
        attendanceCheckin.setEmployeeName(employee.getName());
        attendanceCheckin.setDepartmentId(employee.getDepartmentId());
        attendanceCheckin.setCheckDate(LocalDate.now());
        attendanceCheckin.setCheckTime(LocalTime.now());
        baseMapper.insert(attendanceCheckin);
    }

    public List<AttendanceCheckin> checkRecord(AttendanceCheckin attendanceCheckin) {

        return baseMapper.selectList(new LambdaQueryWrapper<AttendanceCheckin>()
                .eq(AttendanceCheckin::getEmployeeId, attendanceCheckin.getEmployeeId())
                .apply("DATE_FORMAT(check_date, '%Y-%m') = {0}", attendanceCheckin.getCheckMonth()));
    }
}




